package com.wbt.bambootown.ui.adapter;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wbt.bambootown.R;
import com.wbt.bambootown.entity.ControlDevices;
import com.wbt.bambootown.entity.DevicesData;
import com.wbt.bambootown.ui.view.recyclerview.BaseAdapter;
import com.wbt.bambootown.ui.view.recyclerview.BaseViewHolder;
import com.zhy.autolayout.utils.AutoUtils;


/**
 * Created by wufan on 2018/5/16.
 */

public class DevicesControlAdapter extends BaseAdapter<ControlDevices, BaseViewHolder> {

    private Context context;

    public DevicesControlAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent , R.layout.item_devices);
    }

    @Override
    public void bindCustomViewHolder(final BaseViewHolder viewHolder, final int position) {
        AutoUtils.autoSize(viewHolder.itemView);

        final ControlDevices item = getItem(position);
        TextView mTvName = viewHolder.getTextView(R.id.tv_name);
        TextView mTvControlRecord = viewHolder.getTextView(R.id.tv_control_record);
        LinearLayout mLlThree = viewHolder.getView(R.id.ll_three);
        LinearLayout mLlTwo = viewHolder.getView(R.id.ll_two);
        TextView mTvLeft = viewHolder.getTextView(R.id.tv_left);
        TextView mTvRight = viewHolder.getTextView(R.id.tv_right);
        TextView mTvStop = viewHolder.getTextView(R.id.tv_stop);
        TextView mTvOpen = viewHolder.getTextView(R.id.tv_open);
        TextView mTvClose = viewHolder.getTextView(R.id.tv_close);

        Drawable drawableOn = context.getResources().getDrawable(R.drawable.on02);
        drawableOn.setBounds(0, 0, drawableOn.getMinimumWidth(), drawableOn.getMinimumHeight());
        Drawable drawableOFF = context.getResources().getDrawable(R.drawable.off02);
        drawableOFF.setBounds(0, 0, drawableOFF.getMinimumWidth(), drawableOFF.getMinimumHeight());

        mTvName.setText(item.getName());
        if(item.getControlType().equals(controlType.HoldTwoStatus.name()) || item.getControlType().equals(controlType.PluseTwoStatus.name())){
            mLlThree.setVisibility(View.GONE);
            mLlTwo.setVisibility(View.VISIBLE);
            if(item.getValue() == 1){
                //启动
                mTvOpen.setCompoundDrawables(null,drawableOn,null,null);
                mTvClose.setCompoundDrawables(null,drawableOFF,null,null);
            }else if(item.getValue() == 0){
                //停止
                mTvOpen.setCompoundDrawables(null,drawableOFF,null,null);
                mTvClose.setCompoundDrawables(null,drawableOn,null,null);
            }
        }else{
            mLlThree.setVisibility(View.VISIBLE);
            mLlTwo.setVisibility(View.GONE);
            if(item.getValue() == 1){
                //正转
                mTvLeft.setCompoundDrawables(null,drawableOn,null,null);
                mTvRight.setCompoundDrawables(null,drawableOFF,null,null);
                mTvStop.setCompoundDrawables(null,drawableOFF,null,null);
            }else if(item.getValue() == 2){
                //反转
                mTvLeft.setCompoundDrawables(null,drawableOFF,null,null);
                mTvRight.setCompoundDrawables(null,drawableOn,null,null);
                mTvStop.setCompoundDrawables(null,drawableOFF,null,null);
            }else if(item.getValue() == 0){
                //停止
                mTvLeft.setCompoundDrawables(null,drawableOFF,null,null);
                mTvRight.setCompoundDrawables(null,drawableOFF,null,null);
                mTvStop.setCompoundDrawables(null,drawableOn,null,null);
            }
        }

        mTvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , 1);
            }
        });
        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , 2);
            }
        });
        mTvStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , 0);
            }
        });
        mTvOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , 1);
            }
        });
        mTvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , 0);
            }
        });
        mTvControlRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onControlClick.cmdType(item.getId() , -1);
            }
        });
    }

    @Override
    public int getCustomViewType(int position) {
        return 0;
    }

    public onControlClick onControlClick;

    public interface onControlClick {
        public void cmdType(int id, int type);
    }

    public void setListener(onControlClick mListener) {
        this.onControlClick = mListener;
    }

    public enum controlType{
        HoldTwoStatus,
        HoldThreeStatus,
        PluseTwoStatus,
        PluseThreeStatus
    }

}
